using System;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    [RequireComponent(typeof(Camera))]
    public class PlayerSyncDependCamera : MonoBehaviour
    {
        private Camera _camera;

        private void Awake()
        {
            _camera = GetComponent<Camera>();
        }

        private void OnEnable()
        {
            if (LibertyAIUtils.PlayerController)
            {
                var syncCameraComponent = LibertyAIUtils.PlayerController.cameraController.syncCameraComponent;
                syncCameraComponent.syncCamera = _camera;
            }
        }

        private void OnDisable()
        {
            if (LibertyAIUtils.PlayerController)
            {
                var syncCameraComponent = LibertyAIUtils.PlayerController.cameraController.syncCameraComponent;
                if (syncCameraComponent.syncCamera == _camera)
                {
                    syncCameraComponent.syncCamera = null;
                }
            }
        }

        private void LateUpdate()
        {
            if (LibertyAIUtils.PlayerController)
            {
                var syncCameraComponent = LibertyAIUtils.PlayerController.cameraController.syncCameraComponent;
                syncCameraComponent.OnLateUpdate();
            }
        }
    }
}